using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.HighlightLink;
using System.IO;
using NTTHAO.CommonWeb;
using System.Collections.Generic;

public partial class admin_webparts_Plug_ins_HighlightLink_Default : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        string controlName = null;

        if ("linklist".Equals(action, StringComparison.OrdinalIgnoreCase))
        {
            controlName = "LinkList.ascx";
        }
        else if ("addlink".Equals(action, StringComparison.OrdinalIgnoreCase)
            || "editlink".Equals(action, StringComparison.OrdinalIgnoreCase))
        {
            controlName = "LinkInput.ascx";
        }
        else if ("addgroup".Equals(action, StringComparison.OrdinalIgnoreCase)
            || "editgroup".Equals(action, StringComparison.OrdinalIgnoreCase))
        {
            controlName = "GroupInput.ascx";
        }
        else if ("order".Equals(action, StringComparison.OrdinalIgnoreCase))
        {
            this.ChangeOrder();
            Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=linklist&id=" + Request.QueryString["gid"], false);
            return;
        }
        else if ("dellink".Equals(action, StringComparison.OrdinalIgnoreCase))
        {
            try
            {
                this.DeleteLink(Int32.Parse(Request.QueryString["id"]));
            }
            catch (Exception) { }
            Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=linklist&id=" + Request.QueryString["gid"], false);
            return;
        }
        else if ("deletegroup".Equals(action, StringComparison.OrdinalIgnoreCase))
        {
            try
            {
                this.DeleteGroup(Int32.Parse(Request.QueryString["id"]));
            }
            catch (Exception) { }
            Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"], false);
            return;
        }

        if (controlName == null)
        {
            controlName = "GroupList.ascx";
        }

        try
        {
            if (controlName != null)
            {
                Control ctrl = LoadControl(controlName);
                this.Controls.Add(ctrl);
            }
        }
        catch (Exception ex)
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("p");
            ctrl.InnerHtml = ex.Message + "<br /><br />" + ex.StackTrace + "<br /><br />" + ex.Source;

            this.Controls.Clear();
            this.Controls.Add(ctrl);
        }
    }

    private void ChangeOrder()
    {
        try
        {
            HighlightLinkGroup group = HighlightLinkGroup.GetItem(Convert.ToInt32(Request.QueryString["gid"]));
            string ids = Request.QueryString["ids"];
            string orders = Request.QueryString["orders"];

            if (group == null || ids == null || orders == null)
            {
                return;
            }

            string[] linkIds = ids.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
            string[] linkOrders = orders.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < linkIds.Length; i++)
            {
                int linkId = Convert.ToInt32(linkIds[i]);
                int order = Convert.ToInt32(linkOrders[i]);

                HighlightLink link = new HighlightLink(linkId);
                link.Order = order;
                link.Update();
            }
        }
        catch { }
    }

    public void DeleteGroup(int groupId)
    {
        HighlightLinkGroup group = HighlightLinkGroup.GetItem(groupId);
        List<HighlightLink> links = group.Links;
        foreach (HighlightLink link in links)
        {
            link.Delete();
        }
        group.Delete();
        
        string filePath = Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + group.ImagePath);
        if (Directory.Exists(filePath))
        {
            Directory.Delete(filePath, true);
        }
    }

    private void DeleteLink(int linkId)
    {
        HighlightLink link = new HighlightLink(linkId);
        HighlightLinkGroup group = HighlightLinkGroup.GetItem(link.GroupID);

        link.Delete();

        string filePath = Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + group.ImagePath);

        if (link.ImageName.Length > 0 && File.Exists(Path.Combine(filePath, link.ImageName)))
        {
            File.Delete(Path.Combine(filePath, link.ImageName));
        }
        if (link.ThumbImageName.Length > 0 && File.Exists(Path.Combine(filePath, link.ThumbImageName)))
        {
            File.Delete(Path.Combine(filePath, link.ThumbImageName));
        }
    }
}
